#################################################################################
# Estimating Dynamic Ideal Points for State Supreme Courts                      #
# Jason Windett, Jeffrey J. Harden, and Matthew E.K. Hall                       #
# Dynamic ideal point court medians                                             #
# Last update: 4/15/15                                                          # 
#################################################################################
### Packages and Data ###
library(foreign)
library(ggplot2)
library(scales)
library(RColorBrewer)

ip <- read.dta("ip-plots-medians.dta")
ip$Name <- ip$name
ip$State <- ip$stateab2

sn <- sort(unique(ip$stateab2))

ip$statenum <- NA
for(i in 1:length(sn)) ip$statenum[ip$stateab2 == sn[i]] <- i 

## Plot dynamic scaled court medians ##
st1 <- ip[ip$stateab2 %in% sn[1:13], ] 
st2 <- ip[ip$stateab2 %in% sn[14:26], ] 
st3 <- ip[ip$stateab2 %in% sn[27:39], ] 
st4 <- ip[ip$stateab2 %in% sn[40:52], ] 

# AK to ID
cc <- 13
get.palette <- colorRampPalette(brewer.pal(12, "Paired"))

pdf("dyn-scaled1.pdf")

ggplot(st1, aes(year, statemedian, colour = State)) +
 geom_point() +
 geom_line(lwd = 1) + 
 ylab("Ideal Points") + xlab("Year") +
 scale_x_continuous(breaks = pretty_breaks(n = 16)) +
 scale_y_continuous(breaks = pretty_breaks(n = 13)) +
 theme(legend.position = "none") +
 theme(axis.title.x = element_text(vjust = -.5, size = 15), axis.title.y = element_text(vjust = 1.5, size = 15), axis.text.x = element_text(angle = 45, hjust = 1, size = 12), axis.text.y = element_text(size = 12)) +
 geom_text(data = st1[st1$year == 1995, ], aes(label = State), hjust = .75, vjust = -.25) +
 geom_text(data = st1[st1$year == 2003, ], aes(label = State), hjust = .75, vjust = -.25) +
 geom_text(data = st1[st1$year == 2010, ], aes(label = State), hjust = .75, vjust = -.25) +
 scale_color_manual(values = get.palette(cc))
 
dev.off()

# IL to MT
pdf("dyn-scaled2.pdf")
                              
ggplot(st2, aes(year, statemedian, colour = State)) +
 geom_point() +
 geom_line(lwd = 1) + 
 ylab("Ideal Points") + xlab("Year") +
 scale_x_continuous(breaks = pretty_breaks(n = 16)) +
 scale_y_continuous(breaks = pretty_breaks(n = 13)) +
 theme(legend.position = "none") +
 theme(axis.title.x = element_text(vjust = -.5, size = 15), axis.title.y = element_text(vjust = 1.5, size = 15), axis.text.x = element_text(angle = 45, hjust = 1, size = 12), axis.text.y = element_text(size = 12)) +
 geom_text(data = st2[st2$year == 1995, ], aes(label = State), hjust = .75, vjust = -.25) +
 geom_text(data = st2[st2$year == 2003, ], aes(label = State), hjust = .75, vjust = -.25) +
 geom_text(data = st2[st2$year == 2010, ], aes(label = State), hjust = .75, vjust = -.25) +
 scale_color_manual(values = get.palette(cc))

dev.off()
 
# NC to PA
pdf("dyn-scaled3.pdf")

ggplot(st3, aes(year, statemedian, colour = State)) +
 geom_point() +
 geom_line(lwd = 1) + 
 ylab("Ideal Points") + xlab("Year") +
 scale_x_continuous(breaks = pretty_breaks(n = 16)) +
 scale_y_continuous(breaks = pretty_breaks(n = 13)) +
 theme(legend.position = "none") +
 theme(axis.title.x = element_text(vjust = -.5, size = 15), axis.title.y = element_text(vjust = 1.5, size = 15), axis.text.x = element_text(angle = 45, hjust = 1, size = 12), axis.text.y = element_text(size = 12)) +
 geom_text(data = st3[st3$year == 1995 & st3$State != "OK (Crime)" & st3$State != "OK (SC)", ], aes(label = State), hjust = .75, vjust = -.25) +
 geom_text(data = st3[st3$year == 1998 & (st3$State == "OK (Crime)" | st3$State == "OK (SC)"), ], aes(label = State), hjust = .75, vjust = -.25) +
 geom_text(data = st3[st3$year == 2003, ], aes(label = State), hjust = .75, vjust = -.25) +
 geom_text(data = st3[st3$year == 2010, ], aes(label = State), hjust = .75, vjust = -.25) +
 scale_color_manual(values = get.palette(cc))

dev.off()

# RI to WY
pdf("dyn-scaled4.pdf")

ggplot(st4, aes(year, statemedian, colour = State)) +
 geom_point() +
 geom_line(lwd = 1) + 
 ylab("Ideal Points") + xlab("Year") +
 scale_x_continuous(breaks = pretty_breaks(n = 16)) +
 scale_y_continuous(breaks = pretty_breaks(n = 13)) +
 theme(legend.position = "none") +
 theme(axis.title.x = element_text(vjust = -.5, size = 15), axis.title.y = element_text(vjust = 1.5, size = 15), axis.text.x = element_text(angle = 45, hjust = 1, size = 12), axis.text.y = element_text(size = 12)) +
 geom_text(data = st4[st4$year == 1995 & st4$State != "TX (Crime)" & st4$State != "TX (SC)", ], aes(label = State), hjust = .75, vjust = -.25) +
 geom_text(data = st4[st4$year == 1998 & (st4$State == "TX (Crime)" | st4$State == "TX (SC)"), ], aes(label = State), hjust = .75, vjust = -.25) +
 geom_text(data = st4[st4$year == 2003, ], aes(label = State), hjust = .75, vjust = -.25) +
 geom_text(data = st4[st4$year == 2010, ], aes(label = State), hjust = .75, vjust = -.25) +
 scale_color_manual(values = get.palette(cc))
 
 dev.off()